package com.stylefeng.guns.modular.platform.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.common.constant.Enum.BuyerAuditStatusEnum;
import com.stylefeng.guns.common.constant.Enum.BuyerStatusEnum;
import com.stylefeng.guns.core.result.BaseResultVo;
import com.stylefeng.guns.core.result.JsonReturnHelper;
import com.stylefeng.guns.modular.platform.dao.BuyerDemandMapper;
import com.stylefeng.guns.modular.platform.model.BuyerDemand;
import com.stylefeng.guns.modular.platform.service.IBuyerDemandService;

/**
 * <p>
 * 平台买家需求 服务实现类
 * </p>
 *
 * @author stylefeng123
 * @since 2018-07-13
 */
@Service
public class BuyerDemandServiceImpl extends ServiceImpl<BuyerDemandMapper, BuyerDemand> implements IBuyerDemandService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public BaseResultVo addBuyerDemand(BuyerDemand buyerDemand) {
		try{
			buyerDemand.setStatus(BuyerStatusEnum.DJS.getValue());
			buyerDemand.setAuditStatus(BuyerAuditStatusEnum.DSH.getValue());
			buyerDemand.setCreatedBy(String.valueOf(buyerDemand.getUserId()));
			buyerDemand.setGmtCreate(new Date());
			buyerDemand.setLastUpdatedBy(String.valueOf(buyerDemand.getUserId()));
			buyerDemand.setGmtLastUpdate(new Date());
			
			baseMapper.insert(buyerDemand);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return JsonReturnHelper.getFailWithError("服务异常，请稍后重试", "5001");
		}
		return JsonReturnHelper.getSuccessWithNull();
	}

	@Override
	public BaseResultVo deleteBuyerDemandById(Integer buyerDemandId) {
		try{
			baseMapper.deleteById(buyerDemandId);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return JsonReturnHelper.getFailWithError("服务异常，请稍后重试", "5001");
		}
		return JsonReturnHelper.getSuccessWithNull();
	}

	@Override
	public BaseResultVo updateBuyerDemandById(BuyerDemand buyerDemand) {
		try{
			buyerDemand.setLastUpdatedBy(String.valueOf(buyerDemand.getUserId()));
			buyerDemand.setGmtLastUpdate(new Date());
			
			baseMapper.updateById(buyerDemand);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return JsonReturnHelper.getFailWithError("服务异常，请稍后重试", "5001");
		}
		return JsonReturnHelper.getSuccessWithNull();
	}

	@Override
	public BaseResultVo selectBuyerDemandById(Integer buyerDemandId) {
		BuyerDemand buyerDemand = null;
		try{
			buyerDemand = baseMapper.selectById(buyerDemandId);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return JsonReturnHelper.getFailWithError("服务异常，请稍后重试", "5001");
		}
		return JsonReturnHelper.getSuccessWithData(buyerDemand);
	}

	@Override
	public BaseResultVo selectBuyerDemandList(EntityWrapper<BuyerDemand> wp) {
		List<BuyerDemand> list= null;
		try{
			list= baseMapper.selectList(wp);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return JsonReturnHelper.getFailWithError("服务异常，请稍后重试", "5001");
		}
		return JsonReturnHelper.getSuccessWithData(list);
	}

	@Override
	public BaseResultVo selectBuyerDemandPage(Page<BuyerDemand> page, EntityWrapper<BuyerDemand> wp) {
		Page<BuyerDemand> pageList = null;
		try{
			pageList = page.setRecords(baseMapper.selectPage(page, wp));
		}catch(Exception e){
			return JsonReturnHelper.getFailWithError("服务异常，请稍后重试", "5001");
		}
		return JsonReturnHelper.getSuccessWithData(pageList);
	}

}
